﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Application.Admin.Dto.SightListDto>
@{
    ViewBag.subtitle = "景点列表";
    ViewBag.title = "景点列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("~/Scripts/JsHelper.js")" type="text/javascript"></script>
@{Html.Telerik().Grid(Model)
    .Name("SightGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "search"))
        {  
        <label class="customer-label" for="Customers-input">
            景点区域:
        </label>
        @Html.Partial("_SightArea")
        <label class="customer-label" for="Customers-input">
            景点名:
        </label>
        <input type="text" id="searchSight" value="景点名" data-autocomplete="/sight/search" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "add"))
        {
        <a href="/sight?mode=insert" class='t-button  t-grid-add'>添加景点</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "muldelete"))
        {
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a> 
        }
        if (Html.HasUiPermission(this.ViewContext, "exportsight"))
        {
        <a href='javascript:;' id="sightExport" name="sightExport" style="float: right;"
            class='t-button'>导出数据</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "importsight"))
        {
        <a href='javascript:;' id="sightImport" name="sightImport" style="float: right;"
            class='t-button'>导入景点</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.Name).Width(150).Title("景点名称");
        columns.Bound(o => o.Remark).Width(300).Title("景点描述").EditorTemplateName("TextArea");
        columns.Bound(o => o.Lat).Width(80).Title("纬度");
        columns.Bound(o => o.Lon).Width(80).Title("经度");
        columns.Bound(o => o.VouchNum).Width(50).Title("推荐度");
        columns.Bound(o => o.PicCount).Width(50).Title("有图").ReadOnly();

        columns.Bound(o => o.Address).Width(200).Title("地址").Hidden();
        columns.Bound(o => o.AreaId).Width(200).Title("区域Id").Hidden(true);
        columns.Bound(o => o.ClassId).Width(200).Title("分类Id").Hidden(true);
        columns.Bound(o => o.UserId).Width(200).Title("添加用户Id").Hidden(true);
        columns.Bound(o => o.UserName).Width(200).Title("添加用户名").Hidden(true);
        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP地址").Hidden(true).ReadOnly(true);
        columns.Bound(o => o.AddTime).Width(200).Title("添加时间").Hidden(true).ReadOnly(true);
        columns.Bound(o => o.SortNum).Width(200).Title("排序号").Hidden(true);
        columns.Bound(o => o.State).Width(200).Title("状态").Hidden(true);
        columns.Bound(o => o.IsAud).Width(200).Title("通过审核").Hidden(true);
        columns.Bound(o => o.AudTime).Width(200).Title("审核时间").Hidden(true);
        columns.Bound(o => o.IsDelete).Width(200).Title("是否被删除").Hidden(true);
        columns.Bound(o => o.DeleteTime).Width(200).Title("删除时间").Hidden(true);
        columns.Bound(o => o.IsTop).Width(200).Title("是否顶置").Hidden(true);
        columns.Bound(o => o.TopTime).Width(200).Title("顶置时间").Hidden(true);
        columns.Bound(o => o.IsVouch).Width(200).Title("是否推荐").Hidden(true);
        columns.Bound(o => o.VouchTime).Width(200).Title("推荐时间").Hidden(true);
        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "edit"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "detail"))
            {
                comm.Custom("editCutom").Text("编辑详情")
                    .Action("detail", "sight").ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "delete"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(100).Title("编辑/删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxindex", "sight")
        .Insert("add", "sight")
        .Update("edit", "sight")
        .Delete("delete", "sight"))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
@{Html.Telerik().Window()
        .Name("ImportSightWindow")
        .Title("导入景点")
        .Content(@<text>
    @Html.Partial("_SightImport")
    </text>)
    .Width(500)
    .Draggable(true)
    .Modal(true)
    .Visible(false)
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#SightGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchSight").val();
        var areaId = $('#sightAreaId').val();
        if (searchKeyStr == '景点名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr, areaId: areaId });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/sight/muldelete', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#SightGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#SightGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });

        $("#searchSight").focus(function () {
            if (this.value == "景点名") {
                $("#searchSight").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchSight").val("景点名");
            }
        });

        $("#sightImport").click(function (e) {
            e.preventDefault();
            $('#ImportSightWindow').data('tWindow').center().open();
        });

        $('#sightAreaId').change(function () {
            searchChange();
        });
    });
</script>
